package com.xiaoniu.forum.service.impl;

import com.xiaoniu.forum.dao.CommunityCategoryDao;
import com.xiaoniu.forum.dao.CommunityTopicCategoryDao;
import com.xiaoniu.forum.entity.CommunityCategoryEntity;
import com.xiaoniu.forum.entity.CommunityTopicCategoryEntity;
import com.xiaoniu.forum.entity.vo.CommunityTopicOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.Query;

import com.xiaoniu.forum.dao.CommunityDao;
import com.xiaoniu.forum.entity.CommunityEntity;
import com.xiaoniu.forum.service.CommunityService;
import org.springframework.util.StringUtils;


@Service("communityService")
public class CommunityServiceImpl extends ServiceImpl<CommunityDao, CommunityEntity> implements CommunityService {

    @Autowired
    private CommunityCategoryDao categoryDao;

    @Autowired
    private CommunityTopicCategoryDao communityTopicCategoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CommunityEntity> wrapper = new QueryWrapper<>();

        if (params.get("categoryId") != null){
            wrapper.eq("category_id",params.get("categoryId"));
        }

        IPage<CommunityEntity> page = this.page(
                new Query<CommunityEntity>().getPage(params),
                wrapper
        );

        List<CommunityCategoryEntity> categoryList = categoryDao.selectList(null);

        List<CommunityEntity> records = page.getRecords();

        for (CommunityEntity record : records) {
            for (CommunityCategoryEntity category : categoryList) {
                if (StringUtils.pathEquals(record.getCategoryId(),category.getId())){
                    record.setCategoryName(category.getName());
                }
            }
        }

        return new PageUtils(page);
    }


    @Override
    public Integer removeByIds(List<String> ids) {

        QueryWrapper<CommunityTopicCategoryEntity> wrapper = new QueryWrapper<>();

        Integer failCount = 0;

        for (String id : ids) {
            wrapper = new QueryWrapper<>();
            wrapper.eq("community_id",id);
            Integer count = communityTopicCategoryDao.selectCount(wrapper);
            System.out.println(count);
            if (count > 0){
                failCount ++;
            }else {
                this.baseMapper.deleteById(id);
            }
        }

        return failCount;
    }

    @Override
    public List<CommunityTopicOptionVo> getAllWithOptionsBycommunityCategoryId(String communityCategoryId) {

        List<CommunityTopicOptionVo> list = new ArrayList();

        QueryWrapper<CommunityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",communityCategoryId);

        List<CommunityEntity> communityEntityList = baseMapper.selectList(wrapper);

        int index = 0;

        for (CommunityEntity communityEntity : communityEntityList) {
            list.add(new CommunityTopicOptionVo(communityEntity.getId(),communityEntity.getName(),index++));
        }

        return list;
    }

}